
class SessionsController < ApplicationController
  include UsersHelper

  before_filter :require_no_logined, :except => [:destroy, :is_logined, :get_logined_menu, :get_dropdowned_user_menu]
  after_filter :inc_ip_count, :only => [:create, :ajax_login]
  #helper_method :require_recaptcha?

  def new
    store_location request.referrer if session[:return_to].blank? && request.referrer.present?

    set_seo_meta("阳毅门窗官方商城-有爱就有家")

    if mobile?
      render :action => :mobile_new, layout: 'auth'
    else
      render layout: 'auth'
    end
  end

  def create
    #if !require_recaptcha? || simple_captcha_valid?
      login = params[:mobile].strip

      user = User.where(:mobile => login).first

      if user and user.authenticate(params[:password])
        login_as user

        update_browser_uuid user.uuid
        remember_me if params[:remember_me]

        redirect_to get_and_reset_return_url || root_url
      else
        flash[:error] = "用户不存在或登录密码错误"
        redirect_to :back
      end
    #else
      #flash[:error] = "图片验证码不正确"
      #redirect_to :back
    #end
  end

  def get_ajax_login
    render layout: false
  end

  def get_logined_menu
    render layout: false
  end

  def get_dropdowned_user_menu
    render layout: false
  end

  def ajax_login
    if request.xhr?
      if !require_recaptcha? || simple_captcha_valid?
        login = params[:mobile].strip

        user = User.where(:mobile => login).first

        if user and user.authenticate(params[:password])
          login_as user
          remember_me if params[:remember_me]

          render_success("登录成功")
        else
          render_fail("用户名或密码错误", {require_recaptcha: require_recaptcha_for_ajax?})
        end
      else
        render_fail("请输入正确的图片验证码", {require_recaptcha: require_recaptcha_for_ajax?})
      end
    end
  end

  def third_party_create
    data = request.env['omniauth.auth']
    provider, uid = data.provider, data.uid
    if third_party_auth = UserLoginAuthorization.is_bind_provider?(provider, uid)
      user = third_party_auth.user
      login_welcome user
      login_as user
      redirect_to root_url
    else
      data.merge!(
        :niubi_avatar => data['info']['headimgurl'],
        :niubi_nickname => data['info']['nickname']
      )
      Rails.cache.write([provider, uid], data, expires_in: 3.hours)
      redirect_to bind_bullbill_account_accounts_path(:provider => provider, :uid => uid)
    end
  end

  def is_logined
    if current_user.present?
      render_success("已登录")
    else
      render_fail("未登录")
    end
  end

  def destroy
    logout
    redirect_to root_url
  end

  private

  def require_recaptcha_for_ajax?
    ip_count >= 2 && (!mobile?)
  end

  def get_and_reset_return_url
    tmp_return_to = session[:return_to]
    session[:return_to] = nil
    tmp_return_to
  end

  private

  #def inc_ip_count
    #Rails.cache.write "sign_in/#{request.remote_ip}", ip_count + 1, :expires_in => 180.seconds
  #end

  #def ip_count
    #Rails.cache.read("sign_in/#{request.remote_ip}").to_i
  #end

  #def require_recaptcha?
    #ip_count >= 3 && (!mobile?)
  #end
end
